// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#pragma once

#include <memory>

#include <nebula/csv/options.h>  // IWYU pragma: keep
#include <nebula/io/interfaces.h>
#include <nebula/core/record_batch.h>

#include <nebula/types/type.h>
#include <nebula/types/type_fwd.h>
#include <nebula/future/future.h>
#include <nebula/future/thread_pool.h>
#include <turbo/base/macros.h>

namespace nebula::io {
    class InputStream;
}  // namespace nebula::io

namespace nebula::csv {

    /// A class that reads an entire CSV file into a Nebula Table
    class TURBO_EXPORT TableReader {
    public:
        virtual ~TableReader() = default;

        /// read the entire CSV file and convert it to a Nebula Table
        virtual turbo::Result<std::shared_ptr<Table>> read() = 0;

        /// read the entire CSV file and convert it to a Nebula Table
        virtual Future<std::shared_ptr<Table>> read_async() = 0;

        /// create a TableReader instance
        static turbo::Result<std::shared_ptr<TableReader>> create(io::IOContext io_context,
                                                         std::shared_ptr<io::InputStream> input,
                                                         const ReadOptions &,
                                                         const ParseOptions &,
                                                         const ConvertOptions &);
    };

    /// \brief A class that reads a CSV file incrementally
    ///
    /// Caveats:
    /// - For now, this is always single-threaded (regardless of `ReadOptions::use_threads`.
    /// - Type inference is done on the first block and types are frozen afterwards;
    ///   to make sure the right data types are inferred, either set
    ///   `ReadOptions::block_size` to a large enough value, or use
    ///   `ConvertOptions::column_types` to set the desired data types explicitly.
    class TURBO_EXPORT StreamingReader : public RecordBatchReader {
    public:
        virtual ~StreamingReader() = default;

        virtual Future<std::shared_ptr<RecordBatch>> read_next_async() = 0;

        /// \brief Return the number of bytes which have been read and processed
        ///
        /// The returned number includes CSV bytes which the StreamingReader has
        /// finished processing, but not bytes for which some processing (e.g.
        /// CSV parsing or conversion to Nebula layout) is still ongoing.
        ///
        /// Furthermore, the following rules apply:
        /// - bytes skipped by `ReadOptions.skip_rows` are counted as being read before
        /// any records are returned.
        /// - bytes read while parsing the header are counted as being read before any
        /// records are returned.
        /// - bytes skipped by `ReadOptions.skip_rows_after_names` are counted after the
        /// first batch is returned.
        [[nodiscard]] virtual int64_t bytes_read() const = 0;

        /// create a StreamingReader instance
        ///
        /// This involves some I/O as the first batch must be loaded during the creation process
        /// so it is returned as a future
        ///
        /// Currently, the StreamingReader is not async-reentrant and does not do any fan-out
        /// parsing (see ARROW-11889)
        static Future<std::shared_ptr<StreamingReader>> make_async(
                io::IOContext io_context, std::shared_ptr<io::InputStream> input,
                nebula::internal::Executor *cpu_executor, const ReadOptions &, const ParseOptions &,
                const ConvertOptions &);

        static turbo::Result<std::shared_ptr<StreamingReader>> create(
                io::IOContext io_context, std::shared_ptr<io::InputStream> input,
                const ReadOptions &, const ParseOptions &, const ConvertOptions &);
    };

    /// \brief Count the logical rows of data in a CSV file (i.e. the
    /// number of rows you would get if you read the file into a table).
    TURBO_EXPORT
    Future<int64_t> count_rows_async(io::IOContext io_context,
                                   std::shared_ptr<io::InputStream> input,
                                   nebula::internal::Executor *cpu_executor,
                                   const ReadOptions &, const ParseOptions &);

}  // namespace nebula::csv
